const BASE62_CHARS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

// 压缩函数
function compressNumberString(str) {
    if (!/^\d{100}$/.test(str)) throw new Error("输入必须是100位数字字符串");

    // 计算前导零数量
    let leadingZeros = 0;
    while (leadingZeros < 100 && str[leadingZeros] === '0') leadingZeros++;
    
    // 处理剩余数字部分
    const numStr = str.slice(leadingZeros) || '0';
    const num = BigInt(numStr);

    // 转换为Base62
    let base62 = '';
    if (num === 0n) {
        base62 = '0';
    } else {
        let temp = num;
        while (temp > 0n) {
            const remainder = temp % 62n;
            base62 = BASE62_CHARS[remainder] + base62;
            temp = temp / 62n;
        }
    }

    // 编码前导零数量为固定2位Base62
    let encodedLeadingZeros = '';
    let n = leadingZeros;
    do {
        encodedLeadingZeros = BASE62_CHARS[n % 62] + encodedLeadingZeros;
        n = Math.floor(n / 62);
    } while (n > 0);
    encodedLeadingZeros = encodedLeadingZeros.padStart(2, '0');

    return encodedLeadingZeros + base62;
}

// 解压函数
function decompressNumberString(compressed) {
    if (compressed.length < 2) return null;

    // 分离前导零和数字部分
    const encodedLeadingZeros = compressed.slice(0, 2);
    const base62 = compressed.slice(2);

    // 解码前导零数量
    let leadingZeros = 0;
    for (const c of encodedLeadingZeros) {
        leadingZeros = leadingZeros * 62 + BASE62_CHARS.indexOf(c);
    }

    // 解码Base62为BigInt
    let num = 0n;
    for (const c of base62) {
        num = num * 62n + BigInt(BASE62_CHARS.indexOf(c));
    }

    // 生成原始字符串
    const numStr = num === 0n && leadingZeros === 100 ? '' : num.toString();
    const totalLength = leadingZeros + numStr.length;
    if (totalLength !== 100) return null;

    return '0'.repeat(leadingZeros) + numStr.padEnd(100 - leadingZeros, '0');
}